package com.fingard.dsp.bank.directbank.bos01.util;

import sun.misc.BASE64Decoder;
import sun.security.pkcs.ContentInfo;
import sun.security.pkcs.PKCS7;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class SignOpStep {
	String verifyValue = null;

	public SignOpStep(String verifyValue) {
		this.verifyValue = verifyValue;
	}

	public String sign(String serverIP, String serverPort) throws IOException {
		InetAddress addr = null;
		Socket socket = null;
		String verifyheader = null;
		try {
			verifyheader = "<?xml version=\"1.0\" encoding=\"gbk\"?>\n" + "<msg>\n" + "<msg_head>\n"
					+ "<msg_type>0</msg_type>\n" + "<msg_id>1005</msg_id>\n" + "<msg_sn>0</msg_sn>\n"
					+ "<version>1</version>\n" + "</msg_head>\n" + "<msg_body>\n" + "<origin_data_len>"
					+ this.verifyValue.getBytes("GBK").length + "</origin_data_len>\n" + "<origin_data>"
					+ this.verifyValue + "</origin_data>\n" + "</msg_body>\n" + "</msg>";
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		try {

			String verifyIp = serverIP;
			int verifyPort = Integer.parseInt(serverPort);
			// verifyValue=getSendStr(verifyValue);
			// System.out.println("发送的原文包："+verifyValue);
			addr = InetAddress.getByName(verifyIp);
			socket = new Socket(addr, verifyPort);
			BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "GBK"));
			wr.write(verifyheader);
			wr.flush();
			// 接收
			BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
			String line = null;
			StringBuffer sb = new StringBuffer();
			while ((line = rd.readLine()) != null) {
				// System.out.println(line);
				sb.append(line);
			}
			// 处理返回
			String returnStr = sb.toString();
			// System.out.println("返回的密文："+returnStr);

			wr.close();
			rd.close();
			socket.close();
			return returnStr;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		} finally {
		    if (socket != null) {
		        socket.close();
            }
        }
	}

	public static String getNodeValue(String returnStr, String tagName) {
		int startIdx, endIdx;
		String beginTag = "<" + tagName + ">";
		String endTag = "</" + tagName + ">";
		startIdx = returnStr.indexOf(beginTag) + beginTag.length();
		endIdx = returnStr.indexOf(endTag, startIdx);
		return returnStr.substring(startIdx, endIdx);
	}

	public static byte[] base64Decoder(String bytes) {
		if (bytes == null || bytes.length() < 1)
			return "".getBytes();
		byte[] base64Str = null;
		try {
			base64Str = new BASE64Decoder().decodeBuffer(bytes);
		} catch (Exception e) {
			return "".getBytes();
		}
		return base64Str;
	}

	/**
	 * <b>功能描述: </b><br>
	 * <p>
	 * </p>
	 * 
	 * @param args
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 * 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException, IOException {
		// TODO Auto-generated method stub
		// String otest=args[0];
		// String otest="要签名的数据";
		// SignOpStep ss=new SignOpStep(otest);
		// try {
		// String returnStr=ss.sign();
		// String miwen=getNodeValue(returnStr,"signed_data");
		// System.out.println("返回的密文："+miwen);
		// System.out.println("返回的密文长度："+miwen.length());
		//
		// p7 = new PKCS7(SignOpStep.base64Decoder(miwen));
		// info = p7.getContentInfo();
		//
		// String minwen=new String(info.getContentBytes(),"gbk");
		// System.out.println("返回的明文："+minwen);
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

//		String s = "MIIF8AYJKoZIhvcNAQcCoIIF4TCCBd0CAQExCzAJBgUrDgMCGgUAMIHrBgkqhkiG9w0BBwGggd0Egdo8P3htbCB2ZXJzaW9uPSIxLjAiIGVuY29kaW5nPSJnYmsiPz48Qk9TRUJhbmtEYXRhPjxvcFJlcT48c2VyaWFsTm8+MTkwMTA3MTEzMDAwMDAyPC9zZXJpYWxObz48cmVxVGltZT4yMDE5MDEwNzExMzAwMDwvcmVxVGltZT48UmVxUGFyYW0+PHVzZXJJRD4xNzQ3MDQ8L3VzZXJJRD48dXNlclBXRD45NjI4ODg8L3VzZXJQV0Q+PC9SZXFQYXJhbT48L29wUmVxPjwvQk9TRUJhbmtEYXRhPqCCA8QwggPAMIICqKADAgECAhAQAA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YpgNHl4EGgfGCnZfFrob2iMQ06tok+FVrc8ghZCRw=="));
		ContentInfo info = p7.getContentInfo();

		String minwen = new String(info.getContentBytes(), "gbk");
		System.out.println("返回的明文：" + minwen);
	}

}
